var backgroundForestImg = new Image();
var flgDown;
var ctx;
var screenWidth, canvasWidth, xplus;
var screenHeight, canvasHeight, yplus;
var lastHammerX, lastHammerY;
var timer;
var isPause = 0;
var timeImg = new Image();;

function GameObject()
{
	this.x = 0;
	this.y = 0;
	this.flgDown = 0;
	this.hammerImg = null;
	this.hammerDownImg = null;
}

function _hammer(){};
_hammer.prototype = new GameObject();

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}
var GameCtrl = Class.create();
var gameCtrl;
GameCtrl.prototype = {
	initialize: function(obj, options)  {
		this.jimoImg = new Image();
		this.xiaojiaImg = new Image();
		this.hammer = new _hammer();
		this.hammer.hammerDownImg = new Image();
		this.hammer.hammerImg = new Image();
		this.numberImg = new Image();
		this.number = 0;
		this.react = 0;
		this.MAXSIZE = 9;
		this.timeCtrl = 10;
		this.timeCount = 0;
		this.timeOut = 600;
		this.jimocount = 0;
		this.points = new Array([165,157,0.6],
				[275,157,0.6],
				[410,157,0.6],
				[135,213,0.8],
				[280,213,0.8],
				[435,213,0.8],
				[80,287,1],
				[260,287,1],
				[438,287,1]);
		this.holes = new Array([0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]);
		this.loadImages();
	},

	Start: function() {
		this.timer = window.setInterval(GameLoop, 100);
	},

	Stop: function() {
		window.clearInterval(this.timer);
		window.location.href="GameOver.jsp?count="+this.number+"&response="+(this.react/this.number)+"&hitrate="+(this.number/this.jimocount*100); 
	},

	CreateNewHead: function() {
		var num = this.GetRandom(this.MAXSIZE);
		var r = this.GetRandom(2);
		if (r == 0) {
			this.jimocount++;
		}
		if(this.holes[num][0] == 0) { this.holes[num][0] = 1; this.holes[num][1] = r;}
	},

	DrawHeads: function() {	
		for(var i = 0; i < this.MAXSIZE; i++) {
			if(this.holes[i][0] != 0) {
				if(this.holes[i][0] <= -1) {
					var x = 5 * 125;
					var sizex = 125 * this.points[i][2];
					var sizey = 180 * this.points[i][2];
					var img = this.holes[i][1] == 0 ? this.jimoImg : this.xiaojiaImg;

					ctx.drawImage(img,x,0,125,180,this.points[i][0]+xplus,this.points[i][1]+yplus,sizex,sizey);				
				} else if(this.holes[i][0] <= 5) {
					var x = (this.holes[i][0] - 1) * 125;
					var sizex = 125 * this.points[i][2];
					var sizey = 180 * this.points[i][2];
					var img = this.holes[i][1] == 0 ? this.jimoImg : this.xiaojiaImg

					ctx.drawImage(img,x,0,125,180,this.points[i][0]+xplus,this.points[i][1]+yplus,sizex,sizey);
				} else {
					var x = (10 - this.holes[i][0] - 1) * 125;
					var sizex = 125 * this.points[i][2];
					var sizey = 180 * this.points[i][2];
					var img = this.holes[i][1] == 0 ? this.jimoImg : this.xiaojiaImg

					ctx.drawImage(img,x,0,125,180,this.points[i][0]+xplus,this.points[i][1]+yplus,sizex,sizey);
				}
				this.holes[i][0] = (++this.holes[i][0]) % 10;
			}
		}
	},

	GetRandom: function(r) {
		var number = Math.random();
		return Math.floor(number * r);
	},

	loadImages: function()
	{
		this.jimoImg.src = "../img/jimotest.gif";
		this.xiaojiaImg.src = "../img/xiaojia.gif";
		this.hammer.hammerImg.src = "../img/hammer.gif";
		backgroundForestImg.src = "../img/bk.jpg";
		this.hammer.hammerDownImg.src = "../img/hammerDown.gif";
		this.numberImg.src = "../img/numbers.jpg";
	},

	GetOverOn: function(x, y)
	{
		var num;

		if ((x>563+xplus) || (x<80+xplus) || (y<170+yplus) || (y>470+yplus)) { return;}
		if (y<270+yplus) {
			if (x < 250+xplus) num = 1;
			else if (x<370+xplus) num = 2;
			else num = 3;
		} else if (y<370+yplus) {
			if (x<240+xplus) num = 4;
			else if (x<380+xplus) num = 5;
			else num = 6;
		} else {
			if (x<220+xplus) num = 7;
			else if (x<400+xplus) num = 8;
			else num = 9;
		}
		if (this.holes[num - 1][1] != 0) {
			alert("竟然敢打文姜？？不想混了!直接Game Over!!");
			this.Stop();
			return;
		}
		if (this.holes[num - 1][0] != 0) {
			this.number += 1;
			this.react += this.holes[num - 1][0]/10;
			this.holes[num - 1][0] = -3;
		}
	},

	Test: function()
	{
		alert("aaa" + this.hammer.x);
	}
};

function GameLoop()
{
	if (isPause == 1)
		return;
	var percent = gameCtrl.hammer.y / screenHeight;	
	if (gameCtrl.timeCount == gameCtrl.timeOut) {
		gameCtrl.Stop();
	}
	ctx.drawImage(backgroundForestImg, xplus, yplus, canvasWidth, canvasHeight);
	ctx.font = "40pt Arial";
	ctx.fillText("剩余时间："+Math.floor(60-gameCtrl.timeCount/10), xplus+8, yplus+52);
	ctx.fillText("打中："+gameCtrl.number, xplus+canvasWidth-200, yplus+52);
	ctx.font = "15pt Arial";
	var str=gameCtrl.react/gameCtrl.number+"";
	var str1=gameCtrl.number/gameCtrl.jimocount*100+"";
	ctx.fillText("反应时间："+str.substring(0,5)+"打击率："+str1.substring(0,5)+"%", xplus+8, yplus+80);
	//ctx.drawImage(timeImg, 0, 0, timeImg.width, timeImg.height, 64, 0, timeImg.width * (1 - gameCtrl.timeCount / (gameCtrl.timeOut + 1)), timeImg.height);
	
	gameCtrl.DrawHeads();
	if (percent <= 0.3) { percent = 0.3;}
	if (gameCtrl.hammer.x > xplus && gameCtrl.hammer.x < xplus + canvasWidth - gameCtrl.hammer.hammerImg.width / 2&&
		gameCtrl.hammer.y > yplus && gameCtrl.hammer.y < yplus + canvasHeight - gameCtrl.hammer.hammerImg.height / 2) {
		if (gameCtrl.hammer.flgDown == 0) {
			ctx.drawImage(gameCtrl.hammer.hammerImg, 0, 0, gameCtrl.hammer.hammerImg.width, gameCtrl.hammer.hammerImg.height, 
			gameCtrl.hammer.x, gameCtrl.hammer.y, gameCtrl.hammer.hammerImg.width * percent, gameCtrl.hammer.hammerImg.height * percent);
		} else {
			ctx.drawImage(gameCtrl.hammer.hammerDownImg, 0, 0, gameCtrl.hammer.hammerDownImg.width, gameCtrl.hammer.hammerDownImg.height, 
			gameCtrl.hammer.x, gameCtrl.hammer.y, gameCtrl.hammer.hammerDownImg.width * percent, gameCtrl.hammer.hammerDownImg.height * percent);
			gameCtrl.hammer.flgDown = 0;
		}
	}
	if(gameCtrl.timeCount++ % gameCtrl.timeCtrl == 0) {	
		gameCtrl.CreateNewHead();
	}
	lastHammerX = gameCtrl.hammer.x;
	lastHammerY = gameCtrl.hammer.y;
	ctx.restore();
};

function pause() {
	isPause = 1;
}

function restart() {
	isPause = 0;
}

$(window).ready(function(){
$('#submit').click(function () {
	try { 
		document.createElement("canvas").getContext("2d"); 
	} catch (e) { 
		alert("HTML5 Canvas is not supported in your browser. Please use IE9 or FF8.0."); 
		window.location.href="Move.html"; 
	}
	if ($('#username').val() == "" || $('#password').val() == "") {
		alert("用户名或密码不能为空");
	}
	else {
		$.ajax({
			type: "POST",
			url: "/hitjimo/login",
			data: "username=" + escape($('#username').val()) + "&password=" + escape($('#password').val()),
			beforeSend: function () {
				$("#loading").css("display", "block"); 
				$("#login").css("display", "none");
			},
			success: function (msg) {
				$("#loading").hide(); //����loading
				if (msg == "success") {
 					//parent.tb_remove();
 					var wrapper = document.getElementById("wrapper");
					var c = document.getElementById('canvas');

					gameCtrl = new GameCtrl();
					addEventHandlers();
					ctx = c.getContext('2d');
					//width="637" height="521" 
					screenWidth = document.documentElement.scrollWidth;
					screenHeight = document.documentElement.scrollHeight;
					canvasWidth = 637;
					canvasHeight = 521;
					xplus = (screenWidth - canvasWidth) / 2;
					yplus = (screenHeight - canvasHeight) / 2;
					c.setAttribute('width', screenWidth);
			        c.setAttribute('height', screenHeight);
			        
			        /* ���û���������С��λ�ã�Ϊ�������λ�� */
	 				document.getElementById('container').style.display = "block";
					ctx.drawImage(backgroundForestImg, xplus, yplus, canvasWidth, canvasHeight);
					//canvasWidth = $("#canvas").attr("width");
					//canvasHeight = $("#canvas").attr("height");
					//$('.container').css({top:0;left:(screenWidth-100)/2;});
					//screenWidth = parseInt(width);
					//screenHeight = parseInt($("#canvas").attr("height"));
					gameCtrl.Start();
				}
				if (msg == "fail") {
					alert("登录失败");
				}
			},
			complete: function (data) {
				//$("#loading").css("display", "none"); //�����¼����ʾloading�����������
				//$("#login").css("display", "block");
 			},
			error: function (XMLHttpRequest, textStatus, thrownError) {
   			}
		});
	}
});
})
function addEventHandlers()
{
	$("#container").mousemove(function(e){
		gameCtrl.hammer.x = e.pageX - (gameCtrl.hammer.hammerImg.width/2);
		gameCtrl.hammer.y = e.pageY - (gameCtrl.hammer.hammerImg.height/2);
	})
	$("#container").mousedown(function(e){
		gameCtrl.GetOverOn(e.pageX, e.pageY);
		gameCtrl.hammer.flgDown = 1;
	})
}